package com.ruoyi.yl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.yl.domain.*;
import com.ruoyi.yl.mapper.DrugMapper;
import com.ruoyi.yl.mapper.StockManageMapper;
import com.ruoyi.yl.mapper.YlStockDrugMapper;
import com.ruoyi.yl.mapper.YlStockMapper;
import com.ruoyi.yl.service.StockManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: linKe
 * @Date: 2022/2/19 9:29
 * @FileName: StockManageServiceImpl
 * @version: 1.0
 * @Description: 入库管理
 */
@Slf4j
@Service
@Transactional(readOnly = true)
public class StockManageServiceImpl implements StockManageService {
    @Resource
    private StockManageMapper stockManageMapper;
    @Resource
    private YlStockMapper ylStockMapper;
    @Resource
    private YlStockDrugMapper ylStockDrugMapper;
    @Resource
    private DrugMapper drugMapper;
    /**
     * 查看所有入库信息
     */
    @Override
    public List<YlStock> getAll() {
        List<YlStock> all = stockManageMapper.selectList(null);
//        System.out.println(all);
//        for (YlStock stock:all){
//            stock.setStockWorker(1L);
//            stock.setTableMaker(1L);
//            YlEmployeeIds allEmployeeId = stockManageMapper.getAllEmployeeId(stock.getId());
//            //获取入库人员名称
//            String stockWorker=stockManageMapper.getEmployeeNameById(allEmployeeId.getStockId());
//            /// stock.getStockWorker().setName(stockWorker);
//
//            //获取制表人员名称
//            String tableMaker=stockManageMapper.getEmployeeNameById(allEmployeeId.getTableMakerId());
//            ///stock.getTableMaker().setName(tableMaker);
//        }
        return all;
    }

    /**
     * 根据id查入库信息
     */
    @Override
    public  YlStockDrugInfo getOneById(Long id) {
        YlStockDrug stockDrug = ylStockDrugMapper.selectById(id);
        YlDrug ylDrugs = drugMapper.selectById(stockDrug.getDrugId());
        YlStock stock = ylStockMapper.selectById(stockDrug.getStockId());
        stock.setTableMaker(1L);
        stock.setStockWorker(1L);
        stock.setCheckWorker(1L);
        YlStockDrugInfo ylStockDrugInfo = new YlStockDrugInfo().setYlDrug(ylDrugs).setYlStock(stock).setYlStockDrug(stockDrug);
        /// YlEmployeeIds allEmployeeId = stockManageMapper.getAllEmployeeId(id);
        //获取入库人员名称
       /// String stockWorker=stockManageMapper.getEmployeeNameById(allEmployeeId.getStockId());
        /// stock.getStockWorker().setName(stockWorker);

        //获取制表人员名称
        /// String tableMaker=stockManageMapper.getEmployeeNameById(allEmployeeId.getTableMakerId());
        /// stock.getTableMaker().setName(tableMaker);

        //获取审核人员名称
        /// String checkWorker=stockManageMapper.getEmployeeNameById(allEmployeeId.getCheckWorkerId());
        /// stock.getCheckWorker().setName(checkWorker);
        log.info("ylStockDrugInfo:{}",ylStockDrugInfo);
        return ylStockDrugInfo;
    }

    /**
     * 根据审核状态查询入库信息
     */
    @Override
    public List<YlStock> getStocksByCheckStatus(Long id) {
        List<YlStock> list = stockManageMapper.selectList(new QueryWrapper<YlStock>().eq("check_status",id));
        for (YlStock stock:list){
            stock.setStockWorker(1L);
            stock.setTableMaker(1L);
            ///  YlEmployeeIds allEmployeeId = stockManageMapper.getAllEmployeeId(stock.getId());

            //获取入库人员名称
            /// String stockWorker=stockManageMapper.getEmployeeNameById(allEmployeeId.getStockId());
            /// stock.getStockWorker().setName(stockWorker);

            //获取制表人员名称
            /// String tableMaker=stockManageMapper.getEmployeeNameById(allEmployeeId.getTableMakerId());
            /// stock.getTableMaker().setName(tableMaker);
        }
        return list;
    }

    /**
     * 根据入库类型查询入库信息
     */
    @Override
    public List<YlStock> getStocksByType(Long id) {
        List<YlStock> all = stockManageMapper.selectList(new QueryWrapper<YlStock>().eq("stock_type",id));
        for (YlStock stock:all){
            stock.setStockWorker(1L);
            stock.setTableMaker(1L);
            /// YlEmployeeIds allEmployeeId = stockManageMapper.getAllEmployeeId(stock.getId());
            //获取入库人员名称
            /// String stockWorker=stockManageMapper.getEmployeeNameById(allEmployeeId.getStockId());
            /// stock.getStockWorker().setName(stockWorker);

            //获取制表人员名称
            /// String tableMaker=stockManageMapper.getEmployeeNameById(allEmployeeId.getTableMakerId());
            /// stock.getTableMaker().setName(tableMaker);
        }
        return all;
    }

    /**
     * 根据输入框模糊查询入库信息
     */
    @Override
    public List<YlStock> getStocksByLike(String dsc) {
        List<YlStock> all = stockManageMapper.getStocksByLike(dsc);

        for (YlStock stock:all){
            stock.setStockWorker(1L);
            stock.setTableMaker(1L);

            YlEmployeeIds allEmployeeId = stockManageMapper.getAllEmployeeId(stock.getId());
            //获取入库人员名称
            String stockWorker=stockManageMapper.getEmployeeNameById(allEmployeeId.getStockId());
            /// stock.getStockWorker().setName(stockWorker);

            //获取制表人员名称
            String tableMaker=stockManageMapper.getEmployeeNameById(allEmployeeId.getTableMakerId());
            /// stock.getTableMaker().setName(tableMaker);
        }
        return all;
    }

    /**
     * 增加入库信息
     * @param stock
     */
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public int addStock(YlStock stock) {
        return stockManageMapper.insert(stock);
    }

}
